package com.songlanyun.modules.opus.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.exception.BbsException;
import com.songlanyun.modules.opus.model.dto.OpusThemeSystemDTO;
import com.songlanyun.modules.opus.service.OpusThemeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.opus.dao.OpusThemeSystemDao;
import com.songlanyun.modules.opus.entity.OpusThemeSystem;
import com.songlanyun.modules.opus.service.OpusThemeSystemService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("opusThemeSystemService")
public class OpusThemeSystemServiceImpl extends ServiceImpl<OpusThemeSystemDao, OpusThemeSystem> implements OpusThemeSystemService {


    @Resource
    private OpusThemeService opusThemeService;

    @Override
    public OpusThemeSystem create(OpusThemeSystem entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public OpusThemeSystem modify(OpusThemeSystem entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }

    /**
     * 话题分页
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String title = MapUtil.getStr(params, "title");
        IPage<OpusThemeSystem> page = this.page(
                new Query<OpusThemeSystem>().getPage(params),
                new LambdaQueryWrapper<OpusThemeSystem>()
                        .eq(StringUtils.isNotBlank(title), OpusThemeSystem::getTitle, title)
                        .orderByDesc(OpusThemeSystem::getCreateTime)
        );

        page.getRecords().forEach(t->{
            //查询作品数量
            t.setOpusNum(opusThemeService.opusNum(t.getId()));
        });

        return new PageUtils(page);
    }

    /**
     * 添加话题
     *
     * @param dto
     * @return
     */
    @Override
    public OpusThemeSystem add(OpusThemeSystemDTO dto) {
        OpusThemeSystem bbsTheme = new OpusThemeSystem();
        BeanUtil.copyPropertiesIgnoreNull(dto, bbsTheme);
        validateBbsTheme(bbsTheme);
        return this.create(bbsTheme);
    }

    /**
     * 修改话题
     *
     * @param dto
     * @return
     */
    @Override
    public OpusThemeSystem edit(OpusThemeSystemDTO dto) {
        OpusThemeSystem bbsTheme = this.loadById(dto.getId(), true);
        BeanUtil.copyPropertiesIgnoreNull(dto, bbsTheme);
        validateBbsTheme(bbsTheme);
        return modify(bbsTheme);
    }

    /**
     * 删除话题
     *
     * @param id
     */
    @Override
    public void remove(Integer id) {
        this.removeById(id);
    }

    /**
     * 删除话题
     *
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Integer id) {
        //删除话题
        this.remove(id);

        //删除作品与话题的关系
        opusThemeService.removeByTid(id);
    }

    /**
     * 话题列表
     *
     * @return
     */
    @Override
    public List<OpusThemeSystem> loadList() {
        return this.baseMapper.selectList(
                new LambdaQueryWrapper<OpusThemeSystem>()
                        .eq(OpusThemeSystem::getOpen, true)
                        .orderByDesc(OpusThemeSystem::getStick)
                        .orderByAsc(OpusThemeSystem::getOrderIndex)
                        .orderByDesc(OpusThemeSystem::getCreateTime));
    }

    @Override
    public void updateOpusNum(List<Long> themeIdList,Integer num) {
        this.baseMapper.updateOpusNum(themeIdList,num);
    }

    @Override
    public void open(OpusThemeSystemDTO dto) {
        this.update(
                new LambdaUpdateWrapper<OpusThemeSystem>()
                        .eq(OpusThemeSystem::getId, dto.getId())
                        .set(OpusThemeSystem::getOpen, dto.getOpen())
        );
    }

    @Override
    public void stick(OpusThemeSystemDTO dto) {
        this.update(
                new LambdaUpdateWrapper<OpusThemeSystem>()
                        .eq(OpusThemeSystem::getId, dto.getId())
                        .set(OpusThemeSystem::getStick, dto.getStick())
        );
    }

    @Override
    public List<String> listByOpusId(Long oid) {
        return this.baseMapper.listByOpusId(oid);
    }

    /**
     * 按id查询话题
     *
     * @param id
     * @param tw
     * @return
     */
    @Override
    public OpusThemeSystem loadById(Integer id, Boolean tw) {
        OpusThemeSystem obj = this.getById(id);
        if (obj == null && tw) {
            throw new RRException(BbsException.BBS_THEME_NOT_EXISTS);
        }
        return obj;
    }


    private void validateBbsTheme(OpusThemeSystem entity) {
        OpusThemeSystem obj = this.getOne(new LambdaQueryWrapper<OpusThemeSystem>()
                .eq(OpusThemeSystem::getTitle, entity.getTitle())
                .last("limit 1")
        );
        if (obj != null) {
            if (!obj.equals(entity)) {
                throw new RRException(BbsException.BBS_THEME_NAME_REPEAT);
            }
        }
    }


}